
package com.sun.showcase.biz.mall.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.MemberLevelDao;
import com.sun.showcase.biz.mall.service.MemberLevelService;
import com.sun.showcase.client.domain.mall.Member;
import com.sun.showcase.client.domain.mall.MemberLevel;
import com.sun.showcase.client.query.mall.MemberLevelQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.UuidUtil;
@Service("memberLevelService")
@Transactional
public class MemberLevelServiceImpl implements MemberLevelService{
	@Autowired
	private MemberLevelDao memberLevelDao;
	
	public void setMemberLevelDao(MemberLevelDao dao) {
		this.memberLevelDao = dao;
	}

	public DataGrid datagrid(MemberLevelQuery memberLevelQuery) {
		DataGrid j = new DataGrid();
		Pager<MemberLevel> pager  = find(memberLevelQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<MemberLevelQuery> getQuerysFromEntitys(List<MemberLevel> memberLevels) {
		List<MemberLevelQuery> memberLevelQuerys = new ArrayList<MemberLevelQuery>();
		if (memberLevels != null && memberLevels.size() > 0) {
			for (MemberLevel tb : memberLevels) {
				MemberLevelQuery b = new MemberLevelQuery();
				BeanUtils.copyProperties(tb, b);
				memberLevelQuerys.add(b);
			}
		}
		return memberLevelQuerys;
	}

	private Pager<MemberLevel> find(MemberLevelQuery memberLevelQuery) {
		Page<MemberLevel> page = PageHelper.startPage(memberLevelQuery.getPage().intValue(), memberLevelQuery.getRows().intValue());
		List<MemberLevel> list = memberLevelDao.findList(memberLevelQuery);
		Pager<MemberLevel> pager = new Pager<MemberLevel>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public MemberLevel add(MemberLevelQuery memberLevelQuery) {
		MemberLevel t = new MemberLevel();
		BeanUtils.copyProperties(memberLevelQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		memberLevelDao.save(t);
		memberLevelQuery.setId(t.getId());
		return t;
		
	}

	public void update(MemberLevelQuery memberLevelQuery) {
		MemberLevel t = memberLevelDao.getById(memberLevelQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(memberLevelQuery, t);
	    	memberLevelDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(MemberLevelQuery memberLevelQuery) {
	    memberLevelDao.updatePart(memberLevelQuery);
	}

	public void delete(java.lang.String[] ids) {
		memberLevelDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.String[] ids) {
		memberLevelDao.deletePt(ids);
	}

	public MemberLevel get(MemberLevelQuery memberLevelQuery) {
		return memberLevelDao.getById(memberLevelQuery.getId());
	}

	public MemberLevel get(String id) {
		return memberLevelDao.getById(id);
	}

	
	public List<MemberLevelQuery> listAll(MemberLevelQuery memberLevelQuery) {
	    List<MemberLevel> list = memberLevelDao.findList(memberLevelQuery);
		List<MemberLevelQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public void buildTree(Member mem, Member father) {
		MemberLevelQuery q = new MemberLevelQuery();
		q.setMemberId(father.getId());
		q.setOrderColum("parent_level");
		q.setOrderRule("asc");
		List<MemberLevelQuery> list = this.listAll(q);
		MemberLevelQuery add = new MemberLevelQuery();
		add.setMemberId(mem.getId());
		add.setMemberName(mem.getNickName());
		add.setParentId(father.getId());
		add.setParentName(father.getNickName());
		add.setParentLevel(1);
		add.setParentAgentFlag(father.getAgentFlag());
		add.setParentHeadImage(father.getHeadImage());
		add.setMemberHeadImage(mem.getHeadImage());
		this.add(add);
		for(MemberLevelQuery m : list) {
			m.setMemberId(mem.getId());
			m.setMemberName(mem.getNickName());
			m.setMemberHeadImage(mem.getHeadImage());
			m.setParentLevel(m.getParentLevel()+1);
			this.add(m);
		}
	}

	@Override
	public MemberLevel getByMemerIdAndLevel(String memberId, Integer parentLevel) {
		return memberLevelDao.getByMemerIdAndLevel(memberId, parentLevel);
	}
	
	
}
